home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / Macintosh Sample Code / SC.023.FracApp 2.0 / MFracApp.p < prev    next >
Encoding:
Text File  |  1990-04-30  |  10.4 KB  |  235 lines  |  [TEXT/MPS ]

  1. {[j=20/53/1$] PasMat Options}
  2.  
  3. PROGRAM FracApp;
  4.  
  5. {-------------------------------------------------------------------------------------------
  6.  
  7.     Program:    FracApp 2.0
  8.     Unit:        FracApp main program
  9.     File:        MFracApp.p
  10.  
  11.     by Keith Rollin & Bo3b Johnson
  12.     of Apple Macintosh Developer Technical Support
  13.  
  14.     Copyright © 1988-1990 Apple Computer, Inc.
  15.     All rights reserved.
  16.  
  17. --------------------------------------------------------------------------------------------
  18.  
  19.     This is the Main part of the program. Its job is to make sure that we’re running on
  20.     an OK machine and, if so, initialize all the parts of MacApp we need, make an
  21.     application object, and run it. When TFracAppApplication.Run returns, that means that
  22.     the user has selected “Quit”, that everything is shut down, and that all we have to
  23.     do is get out of here. If the machine type is not OK, then we put up an alert saying
  24.     what is wrong.
  25.  
  26. --------------------------------------------------------------------------------------------}
  27.  
  28. { Our main program has to be able to run on any machine, so that we can at
  29.   least tell people that they can’t run on this machine. Therefore, make sure
  30.   that we turn off 68020 and 68881 code generation. }
  31.  
  32.   {$MC68881-}
  33.   {$MC68020-}
  34.  
  35.     USES
  36.         { • MacApp Interfaces • }
  37.         UMacApp, UDialog, UPrinting,
  38.  
  39.         { • ToolBox Interfaces • }
  40.         PaletteMgr, Resources, ToolUtils, Packages, QDOffscreen,
  41.  
  42.         { • FracApp Interfaces • }
  43.         UOffscreen, URectStack, UFracApp;
  44.  
  45.     CONST
  46.         kUnsupportedMac     = - 25001;
  47.         kCompiledWrong        = - 25003;
  48.  
  49.     VAR
  50.         gFracAppApplication: TFracAppApplication;
  51.  
  52. {-------------------------------------------------------------------------------------------}
  53. {$S AInit}
  54.  
  55.     PROCEDURE MyInitUDialog;
  56.  
  57.     { This routine is here to replace the standard InitUDialog that comes with
  58.       MacApp. The standard routine suppresses dead-code stripping on ALL UDialog
  59.       classes. However, I know that I don’t use all of them in FracApp, and so don’t
  60.       mind if the linker dead-code strips some of them. Using this alternate
  61.       InitUDialog routine reclaims about 5.5K in my final program.
  62.  
  63.       So what is all this “dead-code stripping” suppression all about. What code is
  64.       being dead-code stripped, and why is it so important to suppress it?
  65.  
  66.       Dead-code stripping the is act of the linker removing all code that doesn’t
  67.       appear to be accessed in any way. This is normally a good feature, as it
  68.       allows us to link with large libraries (such as MacApp.Lib), taking only what
  69.       we need and leaving everything else behind.
  70.  
  71.       However, if we are creating our views via resources, it’s possible for too
  72.       much of MacApp.Lib or our program to get dead-code stripped. For instance, in
  73.       the view resource for one of my dialogs, I have a reference to TIcon.
  74.       However, nowhere in my program nor MacApp is there an explicit reference to
  75.       TIcon. Sure, there are the definitions for the TIcon methods themselves, but
  76.       nowhere are there any TIcon variables, or TViews being coerced into TIcons.
  77.       This means that the linker will strip out all code that is associated with
  78.       TIcon. When it comes time for MacApp to read in the ‘view’ resource and
  79.       create my dialog, it will fail because all the code’s been stripped.
  80.  
  81.       The solution is to force a reference of some sort to all the views that are
  82.       created via resources. We do this with the following “Member” instructions.
  83.       So that we don’t actually execute those commands (it’s not necessary to
  84.       execute the commands - they just need to exist so the linker sees the
  85.       reference to our view sub-classes), we condition them on the variable called
  86.       “gDeadStripSuppression”. This variable is AWAYS FALSE, so the “IF Member(...)”
  87.       statements will never get executed. So why go through the trouble of making
  88.       a variable if its always going to be FALSE? Well, if we used a constant
  89.       there, the linker would get it into it’s head that it’s found code that will
  90.       NEVER be executed and ... yep ... strip it out. So it chucks out the code
  91.       that is keeping other parts of our code from being stripped out. Which means
  92.       that they end up getting thrown out after all. So we use a variable to
  93.       condition the execution of the following statement, because then the linker
  94.       won’t know for sure if the condition will be TRUE or FALSE.
  95.  
  96.       MyInitUDialog is an exact copy of the standard InitUDialog. I've just
  97.       commented out the commands that suppress dead-code stripping of views
  98.       that I’ll never need. There is a danger to this, though. If someone were
  99.       to take the final version of FracApp and do something like change the
  100.       About Box Dialog to have a TPicture in it, FracApp would fail, as the
  101.       code to handle it would have been removed. But that’s a risk I’m prepared
  102.       to take. }
  103.  
  104.         BEGIN
  105.             IF qTemplateViews THEN BEGIN
  106.  
  107.                 { So the linker doesn’t dead strip these }
  108.  
  109.                 IF gDeadStripSuppression THEN BEGIN
  110.                     IF Member(TObject(NIL), TDialogView) THEN;
  111.                     IF Member(TObject(NIL), TControl) THEN;
  112.                     IF Member(TObject(NIL), TButton) THEN;
  113.                     { IF Member(TObject(NIL), TCheckBox) THEN; }
  114.                     { IF Member(TObject(NIL), TRadio) THEN; }
  115.                     { IF Member(TObject(NIL), TCluster) THEN; }
  116.                     IF Member(TObject(NIL), TIcon) THEN;
  117.                     { IF Member(TObject(NIL), TPicture) THEN; }
  118.                     { IF Member(TObject(NIL), TPopup) THEN; }
  119.                     IF Member(TObject(NIL), TStaticText) THEN;
  120.                     IF Member(TObject(NIL), TEditText) THEN;
  121.                     IF Member(TObject(NIL), TNumberText) THEN;
  122.                     { IF Member(TObject(NIL), TPattern) THEN; }
  123.                 END;
  124.  
  125.                 RegisterStdType('TDialogView', kStdDialogView);
  126.                 RegisterStdType('TControl', kStdControl);
  127.                 RegisterStdType('TButton', kStdButton);
  128.                 { RegisterStdType('TCheckBox', kStdCheckBox); }
  129.                 { RegisterStdType('TRadio', kStdRadio); }
  130.                 { RegisterStdType('TCluster', kStdCluster); }
  131.                 RegisterStdType('TIcon', kStdIcon);
  132.                 { RegisterStdType('TPicture', kStdPicture); }
  133.                 { RegisterStdType('TPopup', kStdPopup); }
  134.                 RegisterStdType('TStaticText', kStdStaticText);
  135.                 RegisterStdType('TEditText', kStdEditText);
  136.                 RegisterStdType('TNumberText', kStdNumberText);
  137.                 { RegisterStdType('TPattern', kStdPattern); }
  138.             END;
  139.  
  140.             gUDialogInitialized := TRUE;
  141.         END;
  142.  
  143. {-------------------------------------------------------------------------------------------}
  144. {$S Main}
  145.  
  146.     BEGIN
  147.  
  148.     { Initialize the standard Macintosh Managers. This does the typical InitGraf
  149.       business, as well as initializing MacApp’s global variables. InitToolBox also
  150.       makes sure that we have at least enough memory to do some intelligent failing
  151.       (for example, in StdAlert below). If there isn’t, MacApp just calls
  152.       ExitToShell. Calling InitToolBox is required in MacApp programs. }
  153.  
  154.         InitToolBox;
  155.  
  156.     { Check to see that we were compiled with the right options. FracApp absolutely
  157.       positively needs to be compiled for Color QuickDraw and an FPU. We take
  158.       advantage of 32-bit Color QuickDraw if it’s around, but we don’t require it.
  159.       If FracApp was compiled with the wrong options, put up a dialog saying so,
  160.       and quit the application. }
  161.  
  162.         IF NOT (qNeedsColorQD & qNeedsFPU) THEN BEGIN
  163.             StdAlert(kCompiledWrong);
  164.             ExitToShell;
  165.         END;
  166.  
  167.     { InitToolBox has filled in a global variable call “gConfiguration”. This
  168.       record contains a lot of information on what is available on the Macintosh we
  169.       are running on. What we do next is call ValidateConfiguration to make sure
  170.       that the machine features this program requires are actually available.
  171.       ValidateConfiguration is self-configuring based on the compiler options you
  172.       used to compile this program. If a feature is missing, ValidateConfiguration
  173.       returns FALSE. For instance, we compile FracApp with the -NeedsColorQD
  174.       option. If we try to run FracApp on a machine that doesn’t have Color
  175.       QuickDraw, then ValidateConfiguration returns FALSE. Otherwise, it returns
  176.       TRUE, and we can safely run on this machine.
  177.  
  178.       We also test here to see if we are running on a machine that has an FPU. We
  179.       don’t require that you actually COMPILE FracApp with the -NeedsFPU option,
  180.       but there are some assembly routines that get compiled with 68881 code, and
  181.       those pieces need to be run on a machine with an FPU. If you don’t put
  182.       -NeedsFPU on the MABuild command line, then ValidateConfiguration won’t check
  183.       for an FPU. Therefore, we do it ourself by hand. }
  184.  
  185.         IF ValidateConfiguration(gConfiguration) & gConfiguration.hasFPU THEN BEGIN
  186.  
  187.         { When a Macintosh application is launched, any windows it puts up will
  188.           initially appear behind the windows of any other running applications being
  189.           run under MultiFinder. This is because MultiFinder hasn’t had a chance yet to
  190.           tell the foremost application that it’s being sent to the back. In order to
  191.           get our application’s windows to the front, we call PullApplicationToFront. }
  192.  
  193.             PullApplicationToFront;
  194.  
  195.         { We now continue with the rest of MacApp’s initialization. Calling InitUMacApp
  196.           is required. It takes a single parameter which is the number of times that
  197.           MoreMasters should be called. InitUPrinting, InitUDialog, and InitUTEView
  198.           setup their respective units, and only need to be called if you will be using
  199.           any of their services. Forgetting to call InitUDialog is a particularly
  200.           common problem that is tricky to detect. Many times, you’ll introduce a
  201.           dialog to your program, and forget to call InitUDialog. You’ll also need to
  202.           call InitUTEView if you use any TEditText items, not just TTEViews. If you
  203.           forget to do any of this, you’ll find that the various views that appear in
  204.           dialogs will get stripped out by the Linker, and your program will crash
  205.           mysteriously. If you have a program that works in Debug mode, but not in
  206.           Non-Debug, then this is most likely the problem. }
  207.  
  208.             InitUMacApp(8);
  209.             InitUPrinting;
  210.             InitUTEView;
  211.             MyInitUDialog;
  212.  
  213.         { Finally, we create our application object, initialize it, and run it. From
  214.           this point on, MacApp is in control. When the user selects Quit from the
  215.           menu, we come back to here, where all we have to do exit. }
  216.  
  217.             New(gFracAppApplication);
  218.             FailNIL(gFracAppApplication);
  219.             gFracAppApplication.IFracAppApplication(kFileType);
  220.             gFracAppApplication.Run;
  221.  
  222.         END
  223.         ELSE BEGIN
  224.  
  225.         { We aren’t running on the right kind of machine. We need at least Color
  226.           QuickDraw and an FPU to run. 32-bit Color QuickDraw is supported, but it’s
  227.           not necessary. If we can’t run on this machine, we show an alert saying why
  228.           not, and quit. We use StdAlert for this (which is the MacApp bottleneck
  229.           procedure for displaying alerts) so that we have control over the specific
  230.           text in the message. }
  231.  
  232.             StdAlert(kUnsupportedMac);
  233.         END;
  234.     END.
  235.